{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0 1 2 3 4 5]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "arr = np.arange(6).reshape(2,3)\n",
    "\n",
    "# 顺序C为行优先排序\n",
    "for i in np.nditer(arr, flags=['external_loop'], order='C'):\n",
    "    print(i)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0 3]\n",
      "[1 4]\n",
      "[2 5]\n"
     ]
    }
   ],
   "source": [
    "# 顺序F为列优先排序\n",
    "for i in np.nditer(arr, flags=['external_loop'], order='F'):\n",
    "    print(i)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 多重索引 \n",
    "- 设置 flags=[\"multi_index\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "value: 0 index:<(0, 0)>\n",
      "value: 1 index:<(0, 1)>\n",
      "value: 2 index:<(0, 2)>\n",
      "value: 3 index:<(1, 0)>\n",
      "value: 4 index:<(1, 1)>\n",
      "value: 5 index:<(1, 2)>\n"
     ]
    }
   ],
   "source": [
    "# 2 维数组 按（x 方向和 y 方向）的序列来唯一定位每一个元素\n",
    "mul_it = np.nditer(arr, flags=['multi_index'])\n",
    "\n",
    "while not mul_it.finished:\n",
    "    print(\"value:\", mul_it[0], \"index:<{}>\".format(mul_it.multi_index))\n",
    "    mul_it.iternext()\n"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 运算与广播"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[10 20 30]]\n",
      "[[10]\n",
      " [20]\n",
      " [30]]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([[20, 30, 40],\n",
       "       [30, 40, 50],\n",
       "       [40, 50, 60]])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr3 = np.array([[10], [20], [30]])\n",
    "arr2 = np.array([[10, 20, 30]])\n",
    "print(arr2)\n",
    "print(arr3)\n",
    "arr2+arr3"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.8 (tags/v3.9.8:bb3fdcf, Nov  5 2021, 20:48:33) [MSC v.1929 64 bit (AMD64)]"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "6fff98fc3b3d81bd655c2cc48858186e4d9e2db7b515bf1c3221888f12a62f87"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
